package com.jh.controller;

import com.jh.vo.InventoryDistributionVO;
import com.jh.vo.SalesTrendVO;
import com.jh.vo.TopSalesVO;
import com.jh.result.Result;
import com.jh.service.HomeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/admin/home")
@Slf4j
@Api(tags = "首页相关接口")
public class HomeController {

    @Autowired
    private HomeService homeService;

    @GetMapping("/sales/trend")
    @ApiOperation("获取销售额趋势")
    public Result<SalesTrendVO> getSalesTrend(
            @ApiParam("开始日期") @RequestParam(required = false) String startDate,
            @ApiParam("结束日期") @RequestParam(required = false) String endDate) {
        log.info("获取销售额趋势数据，开始日期：{}，结束日期：{}", startDate, endDate);
        try {
            SalesTrendVO data = homeService.getSalesTrend(startDate, endDate);
            if (data == null || data.getDates() == null || data.getDates().isEmpty()) {
                return Result.error("未查询到数据");
            }
            return Result.success(data);
        } catch (Exception e) {
            log.error("获取销售额趋势数据失败", e);
            return Result.error("获取销售额趋势数据失败");
        }
    }

    @GetMapping("/inventory/distribution")
    @ApiOperation("获取库存分布")
    public Result<List<InventoryDistributionVO>> getInventoryDistribution() {
        log.info("获取库存分布数据");
        return Result.success(homeService.getInventoryDistribution());
    }

    @GetMapping("/sales/top")
    @ApiOperation("获取销量TOP10")
    public Result<List<TopSalesVO>> getTopSales() {
        log.info("获取销量TOP10数据");
        return Result.success(homeService.getTopSales());
    }
}
